package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.PStoreProductAttrValue;
import com.ruoyi.system.domain.bo.PStoreProductAttrValueBo;
import com.ruoyi.system.domain.vo.PStoreProductAttrValueVo;
import com.ruoyi.system.mapper.PStoreProductAttrValueMapper;
import com.ruoyi.system.service.IPStoreProductAttrValueService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 商品属性值Service业务层处理
 *
 * @author ruoyi
 * @date 2022-08-01
 */
@RequiredArgsConstructor
@Service
public class PStoreProductAttrValueServiceImpl implements IPStoreProductAttrValueService {

    private final PStoreProductAttrValueMapper baseMapper;

    /**
     * 查询商品属性值
     */
    @Override
    public PStoreProductAttrValueVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询商品属性值列表
     */
    @Override
    public TableDataInfo<PStoreProductAttrValueVo> queryPageList(PStoreProductAttrValueBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PStoreProductAttrValue> lqw = buildQueryWrapper(bo);
        Page<PStoreProductAttrValueVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商品属性值列表
     */
    @Override
    public List<PStoreProductAttrValueVo> queryList(PStoreProductAttrValueBo bo) {
        LambdaQueryWrapper<PStoreProductAttrValue> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PStoreProductAttrValue> buildQueryWrapper(PStoreProductAttrValueBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PStoreProductAttrValue> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getProductId() != null, PStoreProductAttrValue::getProductId, bo.getProductId());
        lqw.eq(StringUtils.isNotBlank(bo.getSku()), PStoreProductAttrValue::getSku, bo.getSku());
        lqw.eq(bo.getStock() != null, PStoreProductAttrValue::getStock, bo.getStock());
        lqw.eq(bo.getSales() != null, PStoreProductAttrValue::getSales, bo.getSales());
        lqw.eq(bo.getPrice() != null, PStoreProductAttrValue::getPrice, bo.getPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getImage()), PStoreProductAttrValue::getImage, bo.getImage());
        lqw.eq(bo.getCost() != null, PStoreProductAttrValue::getCost, bo.getCost());
        lqw.eq(StringUtils.isNotBlank(bo.getBarCode()), PStoreProductAttrValue::getBarCode, bo.getBarCode());
        lqw.eq(bo.getOtPrice() != null, PStoreProductAttrValue::getOtPrice, bo.getOtPrice());
        lqw.eq(bo.getWeight() != null, PStoreProductAttrValue::getWeight, bo.getWeight());
        lqw.eq(bo.getVolume() != null, PStoreProductAttrValue::getVolume, bo.getVolume());
        lqw.eq(bo.getBrokerage() != null, PStoreProductAttrValue::getBrokerage, bo.getBrokerage());
        lqw.eq(bo.getBrokerageTwo() != null, PStoreProductAttrValue::getBrokerageTwo, bo.getBrokerageTwo());
        lqw.eq(bo.getType() != null, PStoreProductAttrValue::getType, bo.getType());
        lqw.eq(bo.getQuota() != null, PStoreProductAttrValue::getQuota, bo.getQuota());
        lqw.eq(bo.getQuotaShow() != null, PStoreProductAttrValue::getQuotaShow, bo.getQuotaShow());
        lqw.eq(StringUtils.isNotBlank(bo.getAttrValue()), PStoreProductAttrValue::getAttrValue, bo.getAttrValue());
        return lqw;
    }

    /**
     * 新增商品属性值
     */
    @Override
    public Boolean insertByBo(PStoreProductAttrValueBo bo) {
        PStoreProductAttrValue add = BeanUtil.toBean(bo, PStoreProductAttrValue.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改商品属性值
     */
    @Override
    public Boolean updateByBo(PStoreProductAttrValueBo bo) {
        PStoreProductAttrValue update = BeanUtil.toBean(bo, PStoreProductAttrValue.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PStoreProductAttrValue entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除商品属性值
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public PStoreProductAttrValue queryFontByProductId(Long id, String type) {
        LambdaQueryWrapper<PStoreProductAttrValue> wrapper = new LambdaQueryWrapper<PStoreProductAttrValue>()
            .eq(PStoreProductAttrValue::getProductId, id)
            .eq(PStoreProductAttrValue::getType, type);
        List<PStoreProductAttrValue> attrValues = baseMapper.selectList(wrapper);
        if (CollectionUtil.isNotEmpty(attrValues)) {
            return attrValues.get(0);
        }

        return new PStoreProductAttrValue();
    }

    @Override
    public List<PStoreProductAttrValue> queryFontValueByProductId(Long id, String type) {
        LambdaQueryWrapper<PStoreProductAttrValue> wrapper = new LambdaQueryWrapper<PStoreProductAttrValue>()
            .eq(PStoreProductAttrValue::getProductId, id)
            .eq(PStoreProductAttrValue::getType, type);
        List<PStoreProductAttrValue> attrValues = baseMapper.selectList(wrapper);
        if (CollectionUtil.isNotEmpty(attrValues)) {
            return attrValues;
        }

        return new ArrayList<>();
    }
}
